package cn.wolfcode.cloud.shop.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JSONUtil {

    private static final Logger logger = LoggerFactory.getLogger("JSON_UTIL");

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static String toJsonString(Object obj){
        try {
             return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            logger.error("JSON转换失败",e);
            return "";
        }
    }

    public static <T> T parseObject(String objJSON, Class<T> clazz){
        try {
             return objectMapper.readValue(objJSON, clazz);
        } catch (Exception e) {
            logger.error("对象转换失败",e);
            return null;
        }
    }
}
